package com.shop.web;

import com.shop.dao.IProductDao;
import com.shop.dao.impl.ProductDaoImpl;
import com.shop.entity.Cart;
import com.shop.entity.Product;
import com.shop.entity.User;
import com.shop.service.ICartService;
import com.shop.service.impl.CartServiceImpl;
import com.shop.util.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/cart")
public class CartServlet extends BaseServlet {

    private ICartService cartService = new CartServiceImpl();

    /**
     * 展示购物车所有商品
     *
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected String show(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        User user= (User) session.getAttribute("user");
        List<Cart> carts = cartService.findCartsByUid(user.getUid());
        session.setAttribute("uid", user.getUid());
        req.setAttribute("carts", carts);
        return "forward:/pages/cart/cart.jsp";
    }

    /**
     * 实现购物车清空
     *
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected String clear(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        cartService.clear(user.getUid());
        return "redirect:/pages/cart/cart.jsp";
    }

    /**
     * 实现单件商品的删除
     *
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected String deleteOne(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int cid = WebUtils.parseInt(req.getParameter("cid"), 0);
        //通过用户ID来确定是哪个用户的购物车，通过pid来确定是删除哪件商品；
        cartService.deleteOne(cid);
        //重定向到购物车界面
        return "redirect:/pages/cart/cart.jsp";
    }

    /**
     * 实现商品数量的改变
     *
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected String update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int cid = WebUtils.parseInt(req.getParameter("cid"), 0);
        int cnum = WebUtils.parseInt(req.getParameter("cnum"), 0);
        int ccount = WebUtils.parseInt(req.getParameter("ccount"), 0);

        //通过用户ID来确定是哪个用户的购物车，通过cid,cnum来确定是删除哪件商品；
        cartService.update(cid, cnum, ccount);
        //重定向到购物车界面
        return "redirect:/pages/cart/cart.jsp";
    }

    /**
     * 实现对商品的添加
     *
     * @param req
     * @param resp
     * @return
     * @throws ServletException
     * @throws IOException
     */

    protected String add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pid = WebUtils.parseInt(req.getParameter("pid"), 0);

        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        //向cart中放入各种值
        Cart cart = new Cart();
        cart.setUid(user.getUid());
        cart.setPid(pid);
        cart.setCnum(1);
        cartService.insert(cart);

        return "forward:/pages/cart/cartSuccess.jsp";
    }


}
